home *** CD-ROM | disk | FTP | other *** search
- Path: sargas.omicron.se!usenet
- From: elias@cepheus.omicron.se (Elias Martenson)
- Newsgroups: comp.lang.c
- Subject: Re: returning ptr to struct with ptrs in it?
- Date: 27 Feb 1996 10:31:51 GMT
- Organization: Omicron
- Message-ID: <ELIAS.96Feb27113151@cepheus.omicron.se>
- References: <4gk852INNbp4@faatcrl.faa.gov>
- NNTP-Posting-Host: graffias.omicron.se
- Mime-Version: 1.0
- Content-Type: text/plain; charset=US-ASCII
- Content-Transfer-Encoding: 7bit
- In-reply-to: lbona@saratoga's message of 23 Feb 1996 11:24:18 GMT
-
- In article <4gk852INNbp4@faatcrl.faa.gov> lbona@saratoga (lbona) writes:
-
-
- > When I declare a function that returns a pointer to a struct that contains
- > pointers, the pointer(s) at the end of the struct get set to garbage after
- > being returned. Pointers not at the end are not affected.
- >
- > /* toke is an ascii string read in from a file */
- > BB = *parse_bar(toke);
- >
- > /* at this point BB.g and BB.h are garbage, but all the other fields */
- > /* in BB are correct including BB.d*/
- >
- > }
- >
- >
- > BAR *parse_bar(char toke[]);
- > {
- > BAR bb;
- >
- > memset(&bb,0,sizeof(BAR));
- > /* at this point BB.d == BB.g == BB.h == NULL */
- >
- > /* read data from file and parse - never touch bb.d, bb.g, or bb.h */
- >
- > /* at this point BB.d == BB.g == BB.h == NULL */
- > return(&bb);
- > }
-
- Woow, haven't I seen this one a lot of times...
-
- You are declaring bb in parse_par() as being an auto. That means that it's
- contents are no longer valid when parse_bar() exists. Change it's definition
- from "BAR bb;" to "static BAR bb;" and it'll work.
-
- Regards /
- Elias
-
- --
- Elias Martenson ! When I come up with a good joke,
- elias@omicron.se ! it will be here.
-